/**
 * @file bsp_log.h
 * @brief 日志API
 * @author wangh (wanghuan3037@fiberhome.com)
 * @version 1.0
 * @date 2020-05-12
 * @copyright Copyright (c) 2020  烽火通信
 */
#ifndef __BSP_LOG_H
#define __BSP_LOG_H
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "project_config.h"
#if (LOG_USES_RTT)
#include "SEGGER_RTT.h"
#elif (LOG_USES_UART)
#include "bsp_uart_v2.h"

#if (LOG_UART == 1)
#define DEBUG_UART      UART_1
#elif (LOG_UART == 2)
#define DEBUG_UART      UART_2
#elif (LOG_UART == 3)
#define DEBUG_UART      UART_3
#elif (LOG_UART == 4)
#define DEBUG_UART      UART_4
#elif (LOG_UART == 5)
#define DEBUG_UART      UART_5
#endif
#endif


#if EASYLOG_EN
#include "elog.h"

#else

void log_lock(void);
void log_unlock(void);
void log_hexdump(uint8_t *buf, uint16_t size);

/* 仅打印文件名，取消目录 */
#define filename(x) strrchr(x,'\\')?strrchr(x,'\\')+1:x
//#define filename(x) strrchr(x,'/')?strrchr(x,'/')+1:x		/* Linux环境下使用 */

#if (LOG_USES_RTT)
#define log_a(format,...)	(log_lock(), SEGGER_RTT_printf(0, "[Assert]%s(%d): ", filename(__FILE__), __LINE__), SEGGER_RTT_printf(0, format, ##__VA_ARGS__), log_unlock())
#define log_e(format,...)	(log_lock(), SEGGER_RTT_printf(0, "[Error]%s(%d): ", filename(__FILE__), __LINE__), SEGGER_RTT_printf(0, format, ##__VA_ARGS__), log_unlock())
#define log_w(format,...)	(log_lock(), SEGGER_RTT_printf(0, "[Warn]%s(%d): ", filename(__FILE__), __LINE__), SEGGER_RTT_printf(0, format, ##__VA_ARGS__), log_unlock())
#define log_i(format,...)	do{log_lock(); SEGGER_RTT_printf(0, "[Info]%s(%d): ", filename(__FILE__), __LINE__); SEGGER_RTT_printf(0, format, ##__VA_ARGS__); log_unlock();}while(0)
#define log_d(format,...)	(log_lock(), SEGGER_RTT_printf(0, "[Debug]%s(%d): ", filename(__FILE__), __LINE__), SEGGER_RTT_printf(0, format, ##__VA_ARGS__), log_unlock())
#define log_v(format,...)	(log_lock(), SEGGER_RTT_printf(0, "[Verbose]%s(%d): ", filename(__FILE__), __LINE__), SEGGER_RTT_printf(0, format, ##__VA_ARGS__), log_unlock())
#define log_raw(format,...)	(log_lock(), SEGGER_RTT_printf(0, format, ##__VA_ARGS__), log_unlock())
#define log_printf(format,...)	SEGGER_RTT_printf(0, format, ##__VA_ARGS__)

#elif (LOG_USES_UART)
#define log_a(format,...) (log_lock(), printf("[Assert]%s(%d): ", filename(__FILE__), __LINE__), printf(format, ##__VA_ARGS__), log_unlock())
#define log_e(format,...) (log_lock(), printf("[Error]%s(%d): ", filename(__FILE__), __LINE__), printf(format, ##__VA_ARGS__), log_unlock())
#define log_w(format,...) (log_lock(), printf("[Warn]%s(%d): ", filename(__FILE__), __LINE__), printf(format, ##__VA_ARGS__), log_unlock())
#define log_i(format,...) (log_lock(), printf("[Info]%s(%d): ", filename(__FILE__), __LINE__), printf(format, ##__VA_ARGS__), log_unlock())
#define log_d(format,...) (log_lock(), printf("[Debug]%s(%d): ", filename(__FILE__), __LINE__), printf(format, ##__VA_ARGS__), log_unlock())
#define log_v(format,...) (log_lock(), printf("[Verbose]%s(%d): ", filename(__FILE__), __LINE__), printf(format, ##__VA_ARGS__), log_unlock())
#define log_raw(format,...) (log_lock(), printf(format, ##__VA_ARGS__), log_unlock())
#define log_printf(format,...)	printf(format, ##__VA_ARGS__)

#else
#define log_a(format,...)
#define log_e(format,...)
#define log_w(format,...)
#define log_i(format,...)
#define log_d(format,...)
#define log_v(format,...)
#define log_raw(format,...)
#define log_printf(format,...)
#endif

#if !defined(LOG_LEVEL_)
    #define LOG_LEVEL_ 		4
#endif

#if LOG_LEVEL_ < 1
    #undef log_e
    #define log_e(format,...)
#endif
#if LOG_LEVEL_ < 2
    #undef log_w
    #define log_w(format,...)
#endif
#if LOG_LEVEL_ < 3
    #undef log_i
    #define log_i(format,...)
#endif
#if LOG_LEVEL_ < 4
    #undef log_d
    #define log_d(format,...)
#endif
#if LOG_LEVEL_ < 5
    #undef log_v
    #define log_v(format,...)
#endif

#endif	/* EASYLOG_EN */


void log_init(void);



#endif	 // __BSP_LOG_H 

/*********** (C) COPYRIGHT 2020 FiberHome *****END OF FILE****/
